import React from 'react';
import { Input } from './Input';
import Number from '../../../helpers/Numbers';

/**
 * 输入框-框架
 */
export default class InputFrame extends React.Component {
    constructor(props) {
        super(props);
        this.array = [];
        this.initialization();
    }

    initialization() {
        for (let i = 1; i < 10; i++) {
            let level = [];
            for (let j = 1; j < 10; j++) {
                level.push(0);
            }
            this.array.push(level);
        }
    }

    console() {
        for (let i = 0; i < 9; i++) {
            console.log(this.array[i].toString());
        }
    }

    checkRow(array, row) {
        for (let j = 0; j < 8; j++) {
            if (array[row][j] === 0) {
                continue;
            }
            for (let k = j + 1; k < 9; k++) {
                if (array[row][j] === array[row][k]) {
                    return false;
                }
            }
        }
        return true;
    }
    checkLine(array, col) {
        for (var j = 0; j < 8; j++) {
            if (array[j][col] == 0) {
                continue;
            }
            for (var k = j + 1; k < 9; k++) {
                if (array[j][col] == array[k][col]) {
                    return false;
                }
            }
        }
        return true;
    }
    checkRegion(array, row, col) {
        var j = Math.floor(row / 3) * 3;
        var k = Math.floor(col / 3) * 3;
        for (let i = 0; i < 8; i++) {
            if (array[j + Math.floor(i / 3)][k + i % 3] === 0) {
                continue;
            }
            for (let m = i + 1; m < 9; m++) {
                if (array[j + Math.floor(i / 3)][k + Math.round(i % 3)] === array[j + Math.floor(m / 3)][k + Math.round(m % 3)]) {
                    return false;
                }
            }
        }
        return true;
    }


    isOK(array, row, col) {
        return (this.checkRow(array, row) && this.checkLine(array, col) && this.checkRegion(array, row, col));
    }

    sudoku() {
        let array = this.array;
        for (let i = 0; i < 9; i++) {
            let time = 0;
            for (let j = 0; j < 9; j++) {
                array[i][j] = time === 9 ? 0 : Number.random(1, 9);
                if (array[i][j] === 0) {
                    if (j > 0) {
                        j -= 2;
                        continue;
                    } else {
                        i--;
                        j = 8;
                        continue;
                    }
                }
                if (this.isOK(array, i, j)) {
                    time = 0;
                } else {
                    time++;
                    j--;
                }
            }
        }
        this.console();
    }

    showSudoku() {
        this.sudoku();
        let html = [];
        for (let i = 0; i < 9; i++) {
            for (let j = 0; j < 9; j++) {
                let className = '';
                if ((j + 1) % 3 === 0 && j !== 8) {
                    className += 'line';
                }
                if ((i + 1) % 3 === 0 && i !== 8) {
                    className += ' -row';
                }
                if (Number.random(0, 1) === 1) {
                    html.push(<Input readOnly="readOnly" className={className} defaultValue={this.array[i][j]} key={`${i}${j}`} />);
                } else {
                    html.push(<Input className={className} key={`${i}${j}`} />);
                }
            }
            html.push(<br key={`key:${i}`}/>);
        }
        return (html);
    }

    render() {
        return (
            <div className="sudoku-frame">
                {this.showSudoku()}
            </div>
        );
    }
}
